<cfparam name="FORM.FieldNames" default="">
<cfparam name="FORM.item_name1" default="">
<cfparam name="FORM.payment_status" default="">
<cfparam name="FORM.mc_gross" default="1000000">
<cfparam name="FORM.mc_currency" default="USD">
<cfparam name="FORM.txn_id" default="">
<cfparam name="FORM.receiver_email" default="">
<cfparam name="FORM.payer_email" default="AliDaniali@gmail.com">
<cfparam name="FORM.item_number" default="1">
<cfparam name="FORM.first_name" default="Ali">
<cfparam name="FORM.last_name" default="Daniali">
<!-- read post from PayPal system and add 'cmd' -->
<CFSET str="cmd=_notify-validate">
<CFLOOP INDEX="TheField" list="#Form.FieldNames#">
<CFSET str = str & "&#LCase(TheField)#=#URLEncodedFormat(Form[TheField])#">
</CFLOOP>
<CFIF IsDefined("FORM.payment_date")>
<CFSET str = str & "&payment_date=#URLEncodedFormat(Form.payment_date)#">
</CFIF>
<CFIF IsDefined("FORM.subscr_date")>
<CFSET str = str & "&subscr_date=#URLEncodedFormat(Form.subscr_date)#">
</CFIF>

<!-- post back to PayPal system to validate -->
<!--- THIS IS THE REAL DEAL --->
 <CFHTTP URL="https://www.paypal.com/cgi-bin/webscr?#str#" METHOD="GET" RESOLVEURL="false">
<!--- THIS IS THE TEST DEAL --->
<!--- <CFHTTP URL="https://www.sandbox.paypal.com/cgi-bin/webscr?#str#" METHOD="GET" RESOLVEURL="false"> --->
</CFHTTP>

<!-- assign posted variables to local variables -->
<CFSET item_name=FORM.item_name1>
<CFSET payment_status=FORM.payment_status>
<CFSET payment_amount=FORM.mc_gross>
<CFSET payment_currency=FORM.mc_currency>
<CFSET txn_id=FORM.txn_id>
<CFSET receiver_email=FORM.receiver_email>
<CFSET payer_email=FORM.payer_email>
<CFIF IsDefined("FORM.item_number")>
<CFSET item_number=FORM.item_number>
</CFIF>

<!-- check notification validation -->
<CFIF #CFHTTP.FileContent# is "VERIFIED">
<!-- check that payment_status=Completed -->
<!-- check that txn_id has not been previously processed -->
<!-- check that receiver_email is your Primary PayPal email -->
<!-- check that payment_amount/payment_currency are correct -->
<!-- process payment -->

<CFFILE action="append" addnewline="yes" attributes="normal"  file="#expandpath('/cfpaypal/log/verifiedlog.txt')#" output="#str#" fixnewline="no">
</CFFILE>

<CFFILE action="append" addnewline="yes" attributes="normal"  file="#expandpath('/cfpaypal/log/verifiedlog.txt')#" output="#CFHTTP.FileContent#" fixnewline="no">
</CFFILE>
<!--- UPDATE transaction status code --->
<cfinvoke component="virtualfooddrive-inf.com.donation" method="addAuthenticationResult">
	<cfinvokeargument name="donationID" value="#item_number#">
</cfinvoke>
<!--- output 200 - OK --->
<cfoutput>
	<cfheader statuscode="200" statustext="OK">
</cfoutput>
<!--- ADD EMAIL/PDF STUFF HERE --->
<cfset name = form.first_name +  form.last_name/>
<cfset pdfPath =expandPath("/donations/#item_number#_#DateFormat(Now(),'mm_dd_yyy')#.pdf")> 
<!--- THIS NEEEDS TO PIMPED OUT START --->
<cfoutput>
<cfdocument name="success_email_pdf" filename="#pdfPath#" format="PDF" pagetype="letter" orientation="portrait" backgroundvisible="yes" overwrite="yes" fontembed="yes">
<h1>#name# Thank You!!!</h1>
<p>#payment_amount# #payment_currency#
</cfdocument>
</cfoutput>
<!--- THIS NEEEDS TO PIMPED OUT END--->
<cfmail to="#payer_email#" from="Donations@NorthwestHarvest.com" mimeattach="#pdfPath#" subject="Thank you for your recent donation." ></cfmail>
<CFELSEIF #CFHTTP.FileContent# is "INVALID">
<!-- log for investigation -->
<CFFILE action="append" addnewline="yes" attributes="normal"  file="#expandpath('/cfpaypal/log/invalidlog.txt')#" output="#str#" fixnewline="no">
</CFFILE>

<CFFILE action="append" addnewline="yes" attributes="normal"  file="#expandpath('/cfpaypal/log/invalidlog.txt')#" output="#CFHTTP.FileContent#" fixnewline="no">
</CFFILE>

<CFELSE>
<!-- error -->
</CFIF>